package org.epanetgrid.otimizacao.searchspace;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.epanetgrid.otimizacao.simulation.GradienteNaoConvergeOtimizacaoException;
import org.epanetgrid.otimizacao.simulation.SimulacaoNaoConvergeException;
import org.jheuristics.Individual;


/**
 * Implementa��o default de BulkIndividualEvaluator. Para todos os indiv�duos passados, o evaluator
 * retorna o objeto relacionado correspondente, retornando assim a lista com os indiv�duos "evoluidos",
 * @author vinicius
 *
 */
public class SimpleBulkIndividualEvaluator implements BulkIndividualEvaluator, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -945373159073701001L;
	
	private IndividualEvaluator evaluator;

	/**
	 * @param evaluator Evaluator que analizar� os indiv�duos passados
	 */
	public SimpleBulkIndividualEvaluator(IndividualEvaluator evaluator) {
		if (null == evaluator) {
			throw new NullPointerException();
		}
		this.evaluator = evaluator;
	}

	/* (non-Javadoc)
	 * @see org.smartpumping.otimizacao.searchspace.BulkIndividualEvaluator#evaluate(java.util.List)
	 */
	public List evaluate(List individuals) throws GradienteNaoConvergeOtimizacaoException {
		for (Iterator it = individuals.iterator(); it.hasNext(); ) {
			if (!(it.next() instanceof Individual)) {
				throw new IllegalArgumentException();
			}
		}
		List<Individual> result = new LinkedList<Individual>();
		long i = 1;
		for (Iterator it = individuals.iterator(); it.hasNext(); i++) {
			try {
				result.add(evaluator.evaluate((Individual) it.next()));
			}catch (SimulacaoNaoConvergeException e) {
			}
		}
		return result;
	}

	/**
	 * @return Retorna o IndividuaEvaluator utilizado
	 */
	public IndividualEvaluator getIndividualEvaluator() {
		return evaluator;
	}

}
